/**
 * 
 */
package com.netscenery.yang.model;



import com.netscenery.util.ErrTag;
import com.netscenery.yang.model.YangList.OrderBy;


/**
 * @author llly
 *
 */
public class LeafList extends Node {
	private Type type;
	private String units;
	private int minElements;
	private int maxElements;
	private boolean useDefaultMinElements = true;
	private boolean useDefaultMaxElements = true;
	private OrderBy orderBy;
	public LeafList(Env env,String name) throws ModelException{
		super(env,name);
	}
	
	/**
	 * @return the type
	 */
	public Type getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(Type type) {
		this.type = type;
	}
	/**
	 * @return the units
	 */
	public String getUnits() {
		return units;
	}
	/**
	 * @param units the units to set
	 */
	public void setUnits(String units) {
		this.units = units;
	}
	
	/**
	 * @return the orderBy
	 */
	public OrderBy getOrderBy() {
		return orderBy;
	}

	/**
	 * @param orderBy the orderBy to set
	 */
	public void setOrderBy(OrderBy orderBy) {
		this.orderBy = orderBy;
	}

	public int getMinElements() {
		return minElements;
	}
	/**
	 * @param minElements the minElements to set
	 */
	public void setMinElements(int minElements) {
		this.minElements = minElements;
		this.useDefaultMinElements = false;
	}
	/**
	 * @return the maxElements
	 */
	public int getMaxElements() {
		return maxElements;
	}
	/**
	 * @param maxElements the maxElements to set
	 */
	public void setMaxElements(int maxElements) {
		this.maxElements = maxElements;
		this.useDefaultMaxElements = false;
	}
	/**
	 * @return the useDefaultMinElements
	 */
	public boolean isUseDefaultMinElements() {
		return useDefaultMinElements;
	}

	/**
	 * @return the useDefaultMaxElements
	 */
	public boolean isUseDefaultMaxElements() {
		return useDefaultMaxElements;
	}
	
	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.Node#validate()
	 */
	@Override
	public Validator validate() {		
		Validator validator = new Validator();
		if (null == type){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"the type of leaf node is mandatory."));
		}
		/*
		if (null == getMimInfo()){
			return false;
		}*/
		
		if (!useDefaultMinElements
				&& !useDefaultMaxElements){
			if (minElements > maxElements){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"min-elements is greater than max-elements."));
			}
		}
		
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}
	
	public boolean isMandatory(){
		if (true == useDefaultMinElements){
			return false;
		}
		if (minElements > 0){
			return true;
		}
		
		return false;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
	
}
